package SM.UI.Controls;

import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;

import SM.BusinessLogic.DBAccess;
import SM.BusinessLogic.DBService;

public class DataGrid extends Widget{

	/*Variable Declaration*/
	private Widget Child1;
	private Widget Child2;
	
	/*Constructor*/
	
	public DataGrid()
	{
		super();
	}
	
	public DataGrid(String name, int width)
	{
		super(name, width);
	}
	
	
	public DataGrid(String name, int width, String style)
	{
		super(name, width, style);
	}
	
	/*some Getter setter*/
/*	
	public void SetColumns(int columns)
	{
		this.Columns = columns;
	}
	
	public void SetWidth(int width)
	{
		this.Width = width;
	}
	*/
	
	public void SetChild1(Widget child1)
	{
		this.Child1 = child1;
	}
	
	public void SetChild2(Widget child2)
	{
		this.Child2 = child2;
	}
	
	public void SetChildren(Widget child1, Widget child2)
	{
		this.Child1 = child1;
		this.Child2 = child2;
	}
	
	/*HTML rendering function*/
	
	public String getHTML()
	{
		
		if(this.Child1 instanceof TextBox && this.Child2 instanceof DropDown)
			return HandleMemberData();
		
		return "";
	}
	
	/*Creates 3 result sets
	 * 1. For member name
	 * 2. For responsibilities
	 * 3. For their relationships
	 */
	private String HandleMemberData()
	{
		/*Prolog*/
		Connection connection = DBAccess.MakeConnection();
		
		CallableStatement statementResp = DBAccess.GetStatementForProcedureCall(connection, "ResponsibilitiesSelect()");
		CallableStatement statementRelation = DBAccess.GetStatementForProcedureCall(connection, "MemberResponsibility()");
		
		ResultSet resultSetResp = DBService.ExecuteProcedure(connection, statementResp);
		ResultSet resultSetRelation = DBService.ExecuteProcedure(connection, statementRelation);

		/*Set data in dropdown*/
		
		String Class = (super.GetStyle() == "") ? "default" : super.GetStyle();
		
		if(resultSetRelation == null || resultSetResp == null)
		{
			System.out.println("Null ResultSet in DataGrid.. Cannot fetch data");
			return "";
		}
		
		((DropDown) this.Child2).SetDataSource(resultSetResp, 1, 2);
		((DropDown) this.Child2).DataBind();
		
		String DisplayString = new String();
		
		try
		{
			
			DisplayString += "<table cellspacing=20 name='" + super.GetName()  + "' width='100%' class='" + Class + "'>\n";
			
			while(resultSetRelation.next())
			{
					
					String memberName = resultSetRelation.getString(1);
					String respName = resultSetRelation.getString(2);
					
					((TextBox) this.Child1).SetValue(memberName);
					((DropDown) this.Child2).SetSelectedOption(respName);
					
					DisplayString += "<tr>\n";
				
						DisplayString += "<td>" + this.Child1.getHTML() + "</td>\n";
						DisplayString += "<td>" + this.Child2.getHTML() + "</td>\n";
						
					DisplayString += "</tr>\n";
			}
			
			DisplayString += "</table>";
				
		}catch(SQLException SQLEx)
		{
			System.out.println("Exception in DataGrid\nError : " + SQLEx.getMessage());
			return "";
		}
		finally{
			
			DBAccess.CloseStatement(statementRelation);
			DBAccess.CloseStatement(statementResp);
			
			DBAccess.CloseConnection(connection);
		}
		return DisplayString;
	}
}
